<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Support\Facades\Auth;

class ContractedCustomerModel extends Model
{
    protected $table = 'contracted_customer';

    public function customerSelect(): array
    {
        $model = self::query();

        if (!ISADMIN) {
            if (in_array(Auth::user()->roles->first()->slug, ['boss', 'finance'])) $model->where('contracted_customer.main_id', MAINID);
            else $model->where('contracted_customer.operate_id', USERID);
        }
        $model = $model->get()->toArray();

        $arr = [];
        foreach ($model as $value) {
            $arr[$value['id']] = '合同编号：' . $value['contract_no']
                . ';男方姓名：'
                . $value['male_name'] . ';女方姓名：' . $value['female_name']
                . ';合同金额：' . $value['contract_amount'];
        }

        return $arr;
    }

    public function income(): HasOne
    {
        return self::hasOne(FinanceIncomeModel::class, 'contracted_customer_id', 'id')
            ->where('status', 1);
    }

    public function expense()
    {
        return self::hasOne(FinanceExpensesModel::class, 'contracted_customer_id', 'id')
            ->where('status', 1);
    }
}
